<?php
namespace Com\Pay;
// 各种支付的支付

class Payinterface_baiming{

    private $config = array();

    public function __construct($mchdata){
        $this->config = $mchdata;
    }

    /*生成支付连接*/

    public function getUrl($order_info){
        $price = $order_info['paymoney'];
        $notifyUrl = 'http://' . $_SERVER['HTTP_HOST'].'/Api/NotifyCallback/BaimingPayNotify';
        $returnUrl = $order_info['return_url'];
        $order_sn = $order_info['ordernumber'];
        $order_time = time();
        $passageway_group = intval($_GET['passageway_group']);
        $post_data = array(
            'passageway_group' => $passageway_group, // 0为通道A   1为通道B   2为通道C
            'partner' =>   $this->config['mch_id'],
            'banktype' =>   $order_info['banktype'],
            'ordernumber' =>   $order_sn,
            'paymoney' =>   $price,
            'order_time' => $order_time,
            'ClientIp' =>  get_client_ip(),
            'notify_url' =>   $notifyUrl,
            'href_backurl' =>  $returnUrl,
        );
        if($order_info['banktype'] == 'weixinGzh_pay' || $order_info['banktype'] == 'bank_pay') $post_data['user_id'] = $_REQUEST['user_id'];
        $post_data['sign'] = $this->setSign($post_data,false);
        $Sendurl = 'http://www.xumaxv.cn/index.php/Api/Index/' . $order_info['banktype'];
//        if($order_info['banktype'] != 'bank_pay') {
            $result = $this->https_request($Sendurl, $post_data);
            if(I('get.iszengyuming') == 'imzengyuming'){
                exit(dump($result));
            }
            $res = json_decode($result, true);
            return $res;
//        }else{
//            //银行支付
//            return $Sendurl . '?' . http_build_query($post_data);
//        }
    }

    //回调方法，把返回数据处理成统一规定返回
    public function response(){
        $data = I("post.");
        $Mysign = $this->setSign($data,true);
        if($Mysign === $data['sign'] && $data['orderstatus'] == '1'){
            return true;
        }else{
            // 失败
            return false;
        }
    }

   private function https_request($url, $data = null){
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);

        //参数不为空则为post请求

        if (!empty($data)){

            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        }
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($curl);
        curl_close($curl);
        return $output;
    }

    private function setSign($data,$is_notify = false){
        if(!$is_notify) {
            $md5data = array(
                "partner" => $data['partner'],
                "banktype" => $data['banktype'],
                "ordernumber" => $data['ordernumber'],
                "paymoney" => $data['paymoney'],
                "order_time" => $data['order_time'],
                "notify_url" => $data['notify_url'],
                'ClientIp' => $data['ClientIp'],
            );
            if($data['banktype'] == 'weixin_pay' || $data['banktype'] == 'bank_pay'){
                $md5data['user_id'] = I('request.user_id' , '' , 'intval'); // 如果是微信支付则加入user_id
            }
        }else{
            $md5data = array(
                'partner' => $data['partner'],
                'ordernumber' => $data['ordernumber'],
                'orderstatus' => $data['orderstatus'],
                'bank_type' => $data['bank_type'] ,
                'passageway_id' => $data['passageway_id'], // 渠道商户号
                'passageway_type' => $data['passageway_type'], // 渠道名
                'nonce_str' => $data['nonce_str'],
                'paymoney' => $data['paymoney'],
//                'sysnumber' => $data['sysnumber']
            );
        }
        $str = "";
        foreach($md5data as $key1=>$val)  {
            $str = $str . $key1 . '=' . $val . '&';
        }
        $str = strtoupper(md5($str.'key='.$this->config['mch_key']));
        return $str != "" ? $str : false;
    }
}
